package com.sjy.project.fym.team.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.sjy.framework.aspectj.lang.annotation.Log;
import com.sjy.framework.aspectj.lang.enums.BusinessType;
import com.sjy.project.fym.place.domain.FyPlace;
import com.sjy.project.fym.place.service.IFyPlaceService;
import com.sjy.project.fym.team.domain.FyTeam;
import com.sjy.project.fym.team.service.IFyTeamService;
import com.sjy.framework.web.controller.BaseController;
import com.sjy.framework.web.domain.AjaxResult;
import com.sjy.framework.web.domain.Ztree;
import com.sjy.common.utils.poi.ExcelUtil;
import com.sjy.framework.web.page.TableDataInfo;

/**
 * 防疫队伍Controller
 * 
 * @author sjy
 * @date 2022-04-18
 */
@Controller
@RequestMapping("/fym/team")
public class FyTeamController extends BaseController
{
    private String prefix = "fym/team";

    @Autowired
    private IFyTeamService fyTeamService;
    @Autowired
    private IFyPlaceService fyPlaceService;

    @RequiresPermissions("fym:team:view")
    @GetMapping()
    public String team()
    {
        return prefix + "/team";
    }

    /**
     * 查询防疫队伍列表
     */
    @RequiresPermissions("fym:team:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(FyTeam fyTeam)
    {
        startPage();
        List<FyTeam> list = fyTeamService.selectFyTeamList(fyTeam);
        return getDataTable(list);
    }

    /**
     * 导出防疫队伍列表
     */
    @RequiresPermissions("fym:team:export")
    @Log(title = "防疫队伍", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FyTeam fyTeam)
    {
        List<FyTeam> list = fyTeamService.selectFyTeamList(fyTeam);
        ExcelUtil<FyTeam> util = new ExcelUtil<FyTeam>(FyTeam.class);
        return util.exportExcel(list, "防疫队伍数据");
    }
    /**
     * 导入数据
     */
    @RequiresPermissions("fym:team:import")
    @Log(title = "防疫队伍信息", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<FyTeam> util = new ExcelUtil<FyTeam>(FyTeam.class);
        List<FyTeam> list = util.importExcel(file.getInputStream());
        String message = fyTeamService.importTeam(list, updateSupport);
        return AjaxResult.success(message);
    }
    @RequiresPermissions("fym:team:view")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<FyTeam> util = new ExcelUtil<FyTeam>(FyTeam.class);
        return util.importTemplateExcel("防疫队伍数据");
    }
    /**
     * 新增防疫队伍
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
    	List<FyPlace> fyPlaces = fyPlaceService.selectFyPlaceList(null);
    	mmap.put("fyPlaces", fyPlaces);
        return prefix + "/add";
    }

    /**
     * 新增保存防疫队伍
     */
    @RequiresPermissions("fym:team:add")
    @Log(title = "防疫队伍", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FyTeam fyTeam)
    {
        return toAjax(fyTeamService.insertFyTeam(fyTeam));
    }

    /**
     * 修改防疫队伍
     */
    @RequiresPermissions("fym:team:edit")
    @GetMapping("/edit/{teamId}")
    public String edit(@PathVariable("teamId") Long teamId, ModelMap mmap)
    {
        FyTeam fyTeam = fyTeamService.selectFyTeamByTeamId(teamId);
        mmap.put("fyTeam", fyTeam);

    	List<FyPlace> fyPlaces = fyPlaceService.selectFyPlaceList(null);
    	mmap.put("fyPlaces", fyPlaces);
        return prefix + "/edit";
    }

    /**
     * 修改保存防疫队伍
     */
    @RequiresPermissions("fym:team:edit")
    @Log(title = "防疫队伍", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FyTeam fyTeam)
    {
        return toAjax(fyTeamService.updateFyTeam(fyTeam));
    }

    /**
     * 删除防疫队伍
     */
    @RequiresPermissions("fym:team:remove")
    @Log(title = "防疫队伍", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(fyTeamService.deleteFyTeamByTeamIds(ids));
    }
    
    /**
     * 加载队伍列表树
     */
    @PostMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData(Long placeId,boolean checked)
    {
        List<Ztree> ztrees = fyTeamService.selectTeamTree(new FyTeam(),placeId,checked);
        return ztrees;
    }
}
